import Vue from "vue";
import VueRouter from "vue-router";
import { getToken } from "@/utils/storage";
import { Toast } from "vant";
import Home from "@/views/HomePage.vue";
import RegiSter from "@/views/RegiSter.vue";
import Login from "@/views/LoginCom.vue";

import House from "@/views/HouseCom.vue";
import Mation from "@/views/MationCom.vue";

import Page from "@/views/PageCom.vue";
import MyLogin from "@/views/MyLogin.vue";
import MyCollecTion from "@/views/MyCollecTion.vue";
import MyRental from "@/views/MyRental.vue";
Vue.use(VueRouter);

const routes = [
  {
    path: "/mycollection",
    component: MyCollecTion,
  },
  {
    path: "/myrental",
    component: MyRental,
  },
  {
    path: "/login",
    component: Login,
  },

  {
    path: "/register",
    component: RegiSter,
  },
  {
    path: "/",
    component: Home,
    redirect: "/page",
    children: [
      {
        path: "/page",
        component: Page,
      },
      {
        path: "/house",
        component: House,
      },
      {
        path: "/mation",
        component: Mation,
      },
      {
        path: "/mylogin",
        component: MyLogin,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

const whiteList = ["/House", "/MyLogin", "/mation", "/login", "/register"];

router.beforeEach((to, from, next) => {
  if (getToken()) {
    next();
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      Toast("请登录");
      next("/login");
    }
  }
});

export default router;
